#!/usr/bin/env bash

NOWDIR=$(realpath $(cd "$(dirname "$0")"; pwd))
cd $NOWDIR
pull
source src/config/config.sh
export NODE_ENV=production


cd $NOWDIR/src
rm config.coffee
ln -s config/ol.config.coffee config.coffee
./import.coffee > .import.js


cd $NOWDIR
./sh/yarn.sh

if [ ! -d "$NOWDIR/dist" ]; then
mkdir -p $NOWDIR/dist
fi

cd $NOWDIR/dist
rm -rf *.css *.html *.js *.map

cd $NOWDIR
npx yarn run build
npx coffee sh/dist/hash.coffee

cd $NOWDIR/src
rm -rf config.coffee
ln -s config/dev.config.coffee config.coffee

cd $NOWDIR
coffee -o dist/ -c src/sw/

cd dist

cd $NOWDIR
npx uglifyjs dist/sw.js -cmo dist/sw.js


DIST=/mnt/data/caddy/$HOST
rsync -av $NOWDIR/dist/* $DIST
rsync -av $NOWDIR/static/* $DIST

#cd $NOWDIR/dist
#git add .
#
#sync() {
#    git add -u && git commit -m '.' ;
#    local branch=`git branch 2> /dev/null | sed -e '/^[^*]/d' |awk -F' ' '{print $2}'`
#    git pull origin $branch && git push origin $branch;
#}
#
#sync
